package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.VideoBuyHistoryMapper;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.VideoBuyHistory;
import com.jieshuibao.model.db.VideoBuyHistoryExample;
import com.jieshuibao.util.DateUtils;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class VideoBuyHistoryService{
	
	private static final Logger logger = Logger.getLogger(VideoBuyHistoryService.class);
	
	@Resource
	private VideoBuyHistoryMapper videoBuyHistoryMapper;
	@Resource
	private UserService userService;
	@Resource
	private VideoCardService videoCardService;
	
	public void addVideoBuyHistory(VideoBuyHistory videoBuyHistory) {
		videoBuyHistoryMapper.insertSelective(videoBuyHistory);
	}
	
	public void delVideoBuyHistoryById(String id) {
		videoBuyHistoryMapper.deleteByPrimaryKey(id);
	}
	
	public void updateVideoBuyHistory(VideoBuyHistory videoBuyHistory) {
		videoBuyHistoryMapper.updateByPrimaryKeySelective(videoBuyHistory);
	}
	
	public void updateVideoBuyHistoryByExample(VideoBuyHistory videoBuyHistory,VideoBuyHistoryExample videoBuyHistoryExample){
	    videoBuyHistoryMapper.updateByExampleSelective(videoBuyHistory,videoBuyHistoryExample);
	}
	
	public VideoBuyHistory getVideoBuyHistoryById(String id) {
		return videoBuyHistoryMapper.selectByPrimaryKey(id);
	}
	
	public List<VideoBuyHistory> getPageVideoBuyHistory(VideoBuyHistoryExample videoBuyHistoryExample) {
		return videoBuyHistoryMapper.selectPage(videoBuyHistoryExample);
	}
	
	public int countVideoBuyHistory(VideoBuyHistoryExample videoBuyHistoryExample) {
		return videoBuyHistoryMapper.countByExample(videoBuyHistoryExample);
	}
	
	public List<VideoBuyHistory> selectByExample(VideoBuyHistoryExample example){
	    return videoBuyHistoryMapper.selectByExample(example);
	}
	/**
	 * 余额购买单个视频
	 * @param userid
	 * @param money
	 * @param uuid
	 * @throws Exception
	 */
	public void updateBalanceBuyVideo(String userid,Double money, String uuid) throws Exception {
		userService.updateBalance(userid,money);
		updatePaystatus(uuid,PayCommonUtil.PAYSUCCESS_STATUS);
	}
	/**
	 * 余额购买视频卡
	 * @param userid
	 * @param money
	 * @param uuid
	 * @throws Exception
	 */
	public void updateBalanceBuyCard(String userid,Double money, String uuid) throws Exception {
		userService.updateBalance(userid,money);
		updateVideocardBuy(uuid);
	}
	/**
	 * 修改支付状态
	 * @param uuid  交易id
	 * @param status  交易状态
	 */
	public void updatePaystatus(String uuid,String status) {
		VideoBuyHistory record = new VideoBuyHistory();
		record.setStatus(status);
		record.setId(uuid);
		videoBuyHistoryMapper.updateByPrimaryKeySelective(record );
	}
	/**
	 * 购买会员卡回调业务  修改交易流水 修改会员失效日期
	 * @param productId
	 */
	public void updateVideocardBuy(String bizid) throws Exception{
		VideoBuyHistory videoBuyHistory = videoBuyHistoryMapper.selectByPrimaryKey(bizid);
		if(null == videoBuyHistory){
			logger.error("========不存在购买视频卡业务id========="+bizid);
			throw new Exception("========不存在购买视频卡业务id========="+bizid);
		}
		updatePaystatus(bizid,PayCommonUtil.PAYSUCCESS_STATUS);
		String userId = videoBuyHistory.getUserId();
		Integer validperiod = videoBuyHistory.getValidperiod();
		User userByPhoneNumber = userService.getUserByPhoneNumber(userId);
		if(null == userByPhoneNumber){
			logger.error("=====数据错误不存在用户======="+userId);
			throw new Exception("=====数据错误不存在用户======="+userId);
		}
		Date videoVipInvalidTime = userByPhoneNumber.getVideoVipInvalidTime();
		Date now = new Date();
		if(null == videoVipInvalidTime){
			videoVipInvalidTime = DateUtils.addDays(now, validperiod);
		}else{
			if(videoVipInvalidTime.before(now)){
				videoVipInvalidTime = DateUtils.addDays(now, validperiod);
			}else{
				videoVipInvalidTime = DateUtils.addDays(videoVipInvalidTime, validperiod);
			}
		}
		User user = new User();
		user.setPhoneNumber(userId);
		user.setVideoVipInvalidTime(videoVipInvalidTime);
		userService.updateUser(user );
	}
}
